package com.android.publicModule.webview.download;

import java.util.ArrayList;

import android.content.Context;

import com.android.publicModule.network.DownloadFile;
import com.android.publicModule.network.DownloadProgress;

public class DownloadManage {
	
	private Context mContext;
	
	private ArrayList<DownloadInfo> htmlList;
	
	private ArrayList<DownloadInfo> imgList;
	
	private ArrayList<DownloadInfo> apkList;
	
	private ArrayList<DownloadInfo> fileList;

	public DownloadManage(Context context) {
		if(context!=null)
		{
			mContext = context.getApplicationContext();
		}
	}
	
	public void downloadHtml(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		DownloadInfo info = new DownloadInfo(id,url,savePath,progress,downloadInfo);
		boolean add = false;
		if(htmlList==null)
		{
			htmlList = new ArrayList<DownloadInfo>();
			add = true;
		}
		else if(htmlList.size()==0)
		{
			add = true;
		}
		else if(!htmlList.contains(info))
		{
			add = true;
		}
		if(add)
		{
			htmlList.add(info);
			startDownloadHtml();
		}
	}
	
	private Thread downloadHtmlThread;
	private void startDownloadHtml() {
		if(downloadHtmlThread==null)
		{
			downloadHtmlThread = new Thread() {
				
				public void run() {
					if(mContext!=null)
					{
						DownloadInfo info;
						DownloadFile downloadFile = new DownloadFile(mContext);
						while(htmlList!=null&&htmlList.size()>0)
						{
							info = htmlList.get(0);
							if(info!=null)
							{
								downloadFile.downloadFile(info.getId(), info.getUrl(), info.getSavePath(), info.getProgress(), info.getDownloadInfo());
							}
							try {
								htmlList.remove(0);
							} catch(IndexOutOfBoundsException e) {
								
							} catch(NullPointerException e) {
								
							}
						}
					}
					downloadHtmlThread = null;
				}
				
			};
			try {
				downloadHtmlThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	public void downloadImg(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		DownloadInfo info = new DownloadInfo(id,url,savePath,progress,downloadInfo);
		boolean add = false;
		if(imgList==null)
		{
			imgList = new ArrayList<DownloadInfo>();
			add = true;
		}
		else if(imgList.size()==0)
		{
			add = true;
		}
		else if(!imgList.contains(info))
		{
			add = true;
		}
		if(add)
		{
			imgList.add(info);
			startDownloadImg();
		}
	}
	
	private Thread downloadImgThread;
	private void startDownloadImg() {
		if(downloadImgThread==null)
		{
			downloadImgThread = new Thread() {
				
				public void run() {
					if(mContext!=null)
					{
						DownloadInfo info;
						DownloadFile downloadFile = new DownloadFile(mContext);
						while(imgList!=null&&imgList.size()>0)
						{
							info = imgList.get(0);
							if(info!=null)
							{
								downloadFile.downloadFile(info.getId(), info.getUrl(), info.getSavePath(), info.getProgress(), info.getDownloadInfo());
							}
							try {
								imgList.remove(0);
							} catch(IndexOutOfBoundsException e) {
								
							} catch(NullPointerException e) {
								
							}
						}
					}
					downloadImgThread = null;
				}
				
			};
			try {
				downloadImgThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	public void downloadApk(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		DownloadInfo info = new DownloadInfo(id,url,savePath,progress,downloadInfo);
		boolean add = false;
		if(apkList==null)
		{
			apkList = new ArrayList<DownloadInfo>();
			add = true;
		}
		else if(apkList.size()==0)
		{
			add = true;
		}
		else if(!apkList.contains(info))
		{
			add = true;
		}
		if(add)
		{
			apkList.add(info);
			startDownloadApk();
		}
	}
	
	private Thread downloadApkThread;
	private void startDownloadApk() {
		if(downloadApkThread==null)
		{
			downloadApkThread = new Thread() {
				
				public void run() {
					if(mContext!=null)
					{
						DownloadInfo info;
						DownloadFile downloadFile = new DownloadFile(mContext);
						while(apkList!=null&&apkList.size()>0)
						{
							info = apkList.get(0);
							if(info!=null)
							{
								downloadFile.downloadFile(info.getId(), info.getUrl(), info.getSavePath(), info.getProgress(), info.getDownloadInfo());
							}
							try {
								apkList.remove(0);
							} catch(IndexOutOfBoundsException e) {
								
							} catch(NullPointerException e) {
								
							}
						}
					}
					downloadApkThread = null;
				}
				
			};
			try {
				downloadApkThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	public void downloadFile(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		DownloadInfo info = new DownloadInfo(id,url,savePath,progress,downloadInfo);
		boolean add = false;
		if(fileList==null)
		{
			fileList = new ArrayList<DownloadInfo>();
			add = true;
		}
		else if(fileList.size()==0)
		{
			add = true;
		}
		else if(!fileList.contains(info))
		{
			add = true;
		}
		if(add)
		{
			fileList.add(info);
			startDownloadFile();
		}
	}
	
	private Thread downloadFileThread;
	private void startDownloadFile() {
		if(downloadFileThread==null)
		{
			downloadFileThread = new Thread() {
				
				public void run() {
					if(mContext!=null)
					{
						DownloadInfo info;
						DownloadFile downloadFile = new DownloadFile(mContext);
						while(fileList!=null&&fileList.size()>0)
						{
							info = fileList.get(0);
							if(info!=null)
							{
								downloadFile.downloadFile(info.getId(), info.getUrl(), info.getSavePath(), info.getProgress(), info.getDownloadInfo());
							}
							try {
								fileList.remove(0);
							} catch(IndexOutOfBoundsException e) {
								
							} catch(NullPointerException e) {
								
							}
						}
					}
					downloadFileThread = null;
				}
				
			};
			try {
				downloadFileThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	private DownloadInfo searchDownloadInfo(DownloadInfo info,ArrayList<DownloadInfo> list) {
		DownloadInfo targetInfo = null;
		if(info!=null&&list!=null&&list.size()>0)
		{
			DownloadInfo temp;
			for(int i=0;i<list.size();i++)
			{
				temp = list.get(i);
				if(targetInfo.equals(temp))
				{
					targetInfo = temp;
					break;
				}
			}
		}
		return targetInfo;
	}
	
	public DownloadInfo getDownloadInfo(String id,String url,String fileType) {
		if(fileType==null||fileType.length()==0)
		{
			fileType = "file";
		}
		else
		{
			fileType = fileType.toLowerCase();
		}
		ArrayList<DownloadInfo> list = null;
		DownloadInfo info = new DownloadInfo();
		info.setId(id);
		info.setUrl(url);
		if(fileType.equals("apk"))
		{
			list = apkList;
		}
		else if(fileType.equals("img"))
		{
			list = imgList;
		}
		else if(fileType.equals("html"))
		{
			list = htmlList;
		}
		else
		{
			list = fileList;
		}
		return searchDownloadInfo(info,list);
	}
	
}
